<?php
namespace Admin\Controller;

// 本类由系统自动生成，仅供测试用途
class ConfigController extends CommonController {
	public function _initialize(){
		parent::_initialize();
		$top_nav = array(
			array('name'=>'基础设置','url'=>U('Admin/Config/base'),'tab'=>array('index','base','add')),
			array('name'=>'支付设置','url'=>U('Admin/Config/pay'),'tab'=>array('pay')),
			array('name'=>'微信设置','url'=>U('Admin/Config/wechat'),'tab'=>array('wechat')),
			array('name'=>'登录设置','url'=>U('Admin/Config/login'),'tab'=>array('login')),
			array('name'=>'店铺设置','url'=>U('Admin/Config/shop'),'tab'=>array('shop')),
			array('name'=>'积分设置','url'=>U('Admin/Config/sorce'),'tab'=>array('sorce')),
			array('name'=>'添加配置','url'=>U('Admin/Config/add'),'right'=>1),
		);
		$this->assign('top_nav',$top_nav);
	}

	public function _empty($cate = 'base'){
		if (IS_POST) {
			$data = I('config');
			foreach ($data as $key => $value) {
				M('Config')->where(array('cate'=>$cate,'name'=>$key))->save(array('value'=>$value));
			}
			$data = M('Config')->select();
			foreach ($data as $key => $value) {
				$config[$value['name']] = $value['value'];
			}
			$this->config = $config;
			F('system_config',$this->config);
			$this->success('修改成功！');
		}else{
			$list = M('Config')->where(array('cate'=>$cate))->order('sort asc')->select();
			foreach ($list as $key => $value) {
				if ($value['extra'] != '') {
					$extra = explode(',', $value['extra']);
					foreach ($extra as $k => $v) {
						$extra[$k] = explode(':', $v);
					}
					$value['extra'] = $extra;
				}
				$list[$key] = $value;
			}
			$data = array('list'=>$list,'cate'=>$cate);
			$this->assign($data);
			$this->display('set');
		}
	}

	public function index($cate = 'base'){
		redirect(U('Admin/Config/base'));
	}

	public function add(){
		$config = D('Config');
		if (IS_POST) {
			$data = $config->create();
			if ($data) {
				$result = $config->add();
				if ($result) {
					$this->success("操作成功！");
				}else{
					$this->error($config->getError());
				}
			}else{
				$this->error($config->getError());
			}
		}else{
			$this->display();
		}
	}

	public function edit(){
		$config = D('Config');
		$id = I('id');
		if (IS_POST) {
			$data = $config->create();
			if ($data) {
				$result = $config->where(array('id'=>$id))->save($data);
				if ($result) {
					$this->success("操作成功！");
				}else{
					$this->error($config->getError());
				}
			}else{
				$this->error($config->getError());
			}
		}else{
			$this->display();
		}
	}

	public function unrunfile(){
		//RUNTIME_FILE常量是入口文件中配置的runtimefile的路径及文件名；
		$runflie="./data/~runtime.php";  
		if(file_exists($runflie)){
			unlink($runflie); //删除RUNTIME_FILE;
		}
		//光删除runtime_file还不够，要清空一下Cache文件夹中的文件；代码如下：
		$cachedir="./data/Cache/";   //Cache文件的路径；
		if ($dh = opendir($cachedir)) {     //打开Cache文件夹；
			while (($file = readdir($dh)) !== false) {    //遍历Cache目录，
				unlink($cachedir.$file);                //删除遍历到的每一个文件；
			}
			closedir($dh);
		}    
		$this->success('清除缓存成功',U('Admin/Index/welcome'));
	}	
}